package com.ruoyi.ysg.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ysg.annotation.AddTeacherId;
import com.ruoyi.ysg.domain.YsgSetMeal;
import com.ruoyi.ysg.domain.YsgStudentExtend;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ysg.domain.YsgStudent;
import com.ruoyi.ysg.service.IYsgStudentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.ysg.service.IYsgSetMealService;

/**
 * 学生管理Controller
 *
 * @author xsz
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/ysg/student")
public class YsgStudentController extends BaseController
{
    @Autowired
    private IYsgStudentService ysgStudentService;

    @Autowired
    private IYsgSetMealService ysgSetMealService;

    /**
     * 查询学生管理列表
     */
    @PreAuthorize("@ss.hasPermi('ysg:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(YsgStudent ysgStudent)
    {
        startPage();
        List<YsgStudent> list = ysgStudentService.selectYsgStudentList(ysgStudent);
        return getDataTable(list);
    }


    /**
     * 导出学生管理列表
     */
    @PreAuthorize("@ss.hasPermi('ysg:student:export')")
    @Log(title = "学生管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YsgStudent ysgStudent)
    {
        List<YsgStudent> list = ysgStudentService.selectYsgStudentList(ysgStudent);
        ExcelUtil<YsgStudent> util = new ExcelUtil<YsgStudent>(YsgStudent.class);
        util.exportExcel(response, list, "学生管理数据");
    }

    /**
     * 获取学生管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ysg:student:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ysgStudentService.selectYsgStudentById(id));
    }

    /**
     * 新增学生管理
     */
    @PreAuthorize("@ss.hasPermi('ysg:student:add')")
    @Log(title = "学生管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YsgStudent ysgStudent)
    {
        return toAjax(ysgStudentService.insertYsgStudent(ysgStudent));
    }

    /**
     * 修改学生管理
     */
    @PreAuthorize("@ss.hasPermi('ysg:student:edit')")
    @Log(title = "学生管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YsgStudent ysgStudent)
    {
        return toAjax(ysgStudentService.updateYsgStudent(ysgStudent));
    }

    /**
     * 学生续费
     */
    @PreAuthorize("@ss.hasPermi('ysg:student:edit')")
    @Log(title = "学生管理", businessType = BusinessType.UPDATE)
    @PostMapping("/extend")
    public AjaxResult extend(@RequestBody YsgStudentExtend request)
    {
        // 从请求对象中获取学生信息和套餐信息
        YsgStudent ysgStudent = request.getStudent();
        YsgSetMeal setMeal = request.getSetMeal();

        // 如果套餐信息中只有套餐ID，查询完整的套餐信息
        if (setMeal != null && setMeal.getId() != null) {
            setMeal = ysgSetMealService.selectYsgSetMealById(setMeal.getId());
        }

        return toAjax(ysgStudentService.extend(ysgStudent, setMeal));
    }

    /**
     * 删除学生管理
     */
    @PreAuthorize("@ss.hasPermi('ysg:student:remove')")
    @Log(title = "学生管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ysgStudentService.deleteYsgStudentByIds(ids));
    }

    /**
     * 获取学生可用套餐列表
     */
    @PreAuthorize("@ss.hasPermi('ysg:student:edit')")
    @GetMapping("/availableSetMeals/{studentId}")
    public AjaxResult getAvailableSetMeals(@PathVariable Long studentId)
    {
        return success(ysgSetMealService.selectAvailableSetMealsByStudentId(studentId));
    }
}
